package com.wl.rjwmc.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.rjwmc.DTO.SetmealDto;
import com.wl.rjwmc.DoMain.Category;
import com.wl.rjwmc.DoMain.Setmeal;
import com.wl.rjwmc.DoMain.SetmealDish;
import com.wl.rjwmc.Service.CategoryService;
import com.wl.rjwmc.Service.SetmealDishService;
import com.wl.rjwmc.Service.SetmealService;
import com.wl.rjwmc.Utools.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("setmeal")
@RestController
@Slf4j
public class SetmealDishController {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询和显示
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,lqw);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage);
        List<Setmeal> records  = setmealPage.getRecords();
        List<SetmealDto> setmealDishList = records.stream().map(e->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(e,setmealDto);
            Long categoryId = e.getCategoryId();
            Category category =categoryService.getById(categoryId);
            if (category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDishList);

        return R.success(setmealDtoPage);

    }

    /**
     * 增
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(
            @PathVariable("status") int status,
            @RequestParam List<Long> ids
    ){
        List<Setmeal> setmeals = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmeals.add(setmeal);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("套餐状态修改成功");
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 修改套餐数据回显
     * @param id 接收传入数据回显的ID
     * @return 返回数据联调对象
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealBackData(@PathVariable Long id){
        //调用自定义查询方法
        SetmealDto setmealDto = setmealService.setmealBackData(id);
        return R.success(setmealDto);
    }

    /**
     * 更新套餐信息
     * @param setmealDto 前端传入数据
     * @return 返回数据联调对象
     */
    @PutMapping
    public R<String> setmealUpdate(@RequestBody SetmealDto setmealDto){
        log.info("======={}",setmealDto);
        setmealService.setmealUpdate(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
